package com.wave.system.thread;

import com.wave.common.utils.StringUtils;
import com.wave.system.domain.SysMechanicalArmInfo;
import com.wave.system.runner.SystemApplicationRunner;
import com.wave.system.util.Modbus4jUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RobotStatusListenerThread implements Runnable {

    /**
     * 首页查询机械臂状态
     */
    @Override
    public void run() {
        while (true) {
            try {
                String str = Modbus4jUtils.readHoldingRegistersToString(1, 377, 12);//取料，下料
                boolean[] arm1Plc = Modbus4jUtils.batchReadCoilStatus(1, 4936, 6);
                SysMechanicalArmInfo arm1 = SystemApplicationRunner.armList.get(0);
                if (arm1Plc[0]) {
                    arm1.setArmTaskState("空闲中");
                }
                if (arm1Plc[1]) {
                    arm1.setArmTaskState("取料中");
                }
                if (arm1Plc[2]) {
                    arm1.setArmTaskState("下料中");
                }
                if (arm1Plc[3]) {
                    arm1.setConnectMode("1");
                }
                if (!arm1Plc[3]) {
                    arm1.setConnectMode("2");
                }
                if (arm1Plc[4]) {
                    arm1.setRunningState("工作中");
                }
                if (!arm1Plc[4]) {
                    arm1.setRunningState("空闲中");
                }
                if (arm1Plc[5]) {
                    arm1.setFaultCode("异常");
                }
                if (!arm1Plc[5]) {
                    arm1.setFaultCode("正常");
                }
                if (StringUtils.isNotEmpty(str) && str.length() >= 24) {
                    String pickupLocation = str.substring(2, 6);
                    arm1.setPickupLocation(pickupLocation);
                    String deliveryLocation = str.substring(8, 12);
                    arm1.setDeliveryLocation(deliveryLocation);
                }


                boolean[] arm2Plc = Modbus4jUtils.batchReadCoilStatus(1, 4928, 6);
                SysMechanicalArmInfo arm2 = SystemApplicationRunner.armList.get(1);
                if (arm2Plc[0]) {
                    arm2.setArmTaskState("空闲中");
                }
                if (arm2Plc[1]) {
                    arm2.setArmTaskState("取料中");
                }
                if (arm2Plc[2]) {
                    arm2.setArmTaskState("下料中");
                }
                if (arm2Plc[3]) {
                    arm2.setConnectMode("1");
                }
                if (!arm2Plc[3]) {
                    arm2.setConnectMode("2");
                }
                if (arm2Plc[4]) {
                    arm2.setRunningState("工作中");
                }
                if (!arm2Plc[4]) {
                    arm2.setRunningState("空闲中");
                }
                if (arm2Plc[5]) {
                    arm2.setFaultCode("异常");
                }
                if (!arm2Plc[5]) {
                    arm2.setFaultCode("正常");
                }
                if (StringUtils.isNotEmpty(str) && str.length() >= 24) {
                    String pickupLocation = str.substring(14, 18);
                    arm2.setPickupLocation(pickupLocation);
                    String deliveryLocation = str.substring(20, 24);
                    arm2.setDeliveryLocation(deliveryLocation);
                }
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (Exception e) {
                log.error("更新首页机械臂信息定时任务异常", e);
            }
        }
    }
}
